package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Link;
import com.example.service.LinkService;
import com.example.mapper.LinkMapper;
import com.example.utils.ResponseResult;
import com.example.utils.SecurityUtils;
import com.example.vo.PageVo;
import com.qiniu.util.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author sprite-pc
* @description 针对表【sg_link(友链)】的数据库操作Service实现
* @createDate 2023-12-23 16:19:27
*/
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService{
    @Autowired
    private LinkMapper linkMapper;

    @Override
    public ResponseResult getAllLink() {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus,"1");
        return ResponseResult.okResult(linkMapper.selectList(queryWrapper));
    }

    @Override
    public ResponseResult getList(int pageNum, int pageSize, String name, String status) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        if (name!=null){
            queryWrapper.like(Link::getName,name);
        }
        if (!StringUtils.isNullOrEmpty(status)){
            queryWrapper.eq(Link::getStatus,status);
        }
        Page<Link> pageInfo  = new Page<>(pageNum,pageSize);
        page(pageInfo,queryWrapper);
        PageVo pageVo = new PageVo(pageInfo.getRecords(),pageInfo.getTotal());
        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult saveLink(Link link) {
        link.setCreateBy(SecurityUtils.getUserId());
        link.setCreateTime(new Date());
        save(link);
        return ResponseResult.okResult();
    }

}




